package server;

import common.Request;
import common.Response;

import java.io.*;
import java.net.Socket;

public class ServerThread extends Thread {
    private Socket s;
    public ServerThread(Socket s){
        this.s=s;
    }
    public Response login(Request req){
        Response resp=new Response();
        String name=(String)req.get("username");
        String pass=(String)req.get("userpass");
        if("admin".equals(name)&&"111111".equals(pass)){
            resp.setSuccess(true);
        }else{
            resp.setSuccess(false);
            resp.setMsg("错误的用户名或者密码");
        }
        return resp;
    }

    @Override
    public void run() {
        System.out.println(s+"连接了...");
        try {
            InputStream in=s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectInputStream oin=new ObjectInputStream(in);
            ObjectOutputStream oout=new ObjectOutputStream(out);
            while(true){
                Request req= (Request) oin.readObject();
                Response resp=null;
                if(req.getAction().equals("login")){
                    resp=login(req);
                }else if(req.getAction().equals("register")){

                }else{
                    resp=new Response();
                    resp.setSuccess(false);
                    resp.setMsg("未知操作");
                }
                oout.writeObject(resp);
                oout.reset();//防止发送上次的对象
            }


        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                System.out.println(s+"退出了");
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
